package com.xiang.mall.platform.uc.service.impl

import com.xiang.mall.framework.core.utils.BeanUtilsEx
import com.xiang.mall.platform.uc.entity.UserEntity
import com.xiang.mall.platform.uc.model.dto.UserInfoDto
import com.xiang.mall.platform.uc.repository.UserRepository
import com.xiang.mall.platform.uc.service.UserService
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service

@Service
class UserServiceImpl(
    private val userRepository: UserRepository
) : UserService {

    /**
     * 添加用户
     */
    override fun add(user: UserInfoDto): Boolean {
        if (userRepository.existsByUsernameOrMobileNoOrEmail(user.username, user.mobileNo, user.email)) {
            return false
        }
        val entity = BeanUtilsEx.copyProperties(user, UserEntity::class)
        return userRepository.save(entity).id > 0
    }

    /**
     * 通过用户名查找用户信息
     */
    @Cacheable(cacheNames = ["user.service.findUserByUsername"])
    override fun findUserByUsername(username: String): UserInfoDto? {
        val entity = userRepository.findByUsername(username)

        if (entity != null) {
            return BeanUtilsEx.copyProperties(entity, UserInfoDto::class)
        }
        return null
    }
}